package com.hnguigu.realm;

import com.hnguigu.pojo.Users;
import com.hnguigu.service.AccService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

/**/
public class UserRealm extends AuthorizingRealm {
    @Autowired
    AccService service;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String name = principalCollection.getPrimaryPrincipal().toString();
         HashSet<String> roles = new HashSet<String>();
         //权限数组
         Set<String> permissions=new HashSet<String>();
        //如果当前用户是siniu 就会给他一个admin角色,其他用户就是user角色
        if(name.equals("siniu")){
            permissions.add("user:del");
            permissions.add("user:find");
            roles.add("admin");
        }
        roles.add("user");
        System.out.println("角色数组"+roles);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
        info.setStringPermissions(permissions);
        return  info;
    }
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取当前账号
        Object principal = authenticationToken.getPrincipal();
        System.out.println("reamls账号为"+principal);
        Users users = service.selectByPrimaryKey((String) principal);
        System.out.println("根据用户名为11111111"+users);
        //计算盐值
        ByteSource bytes = ByteSource.Util.bytes(principal);
        System.out.println("用户名的盐值"+bytes);
        //获取当前密码
//        SimpleAuthenticationInfo simple = new SimpleAuthenticationInfo(principal,users.getPassword(),bytes,getName());
        SimpleAuthenticationInfo simple = new SimpleAuthenticationInfo(principal,"1234",getName());
        System.out.println("realm的simple"+simple);
        return simple;
    }
//    public SimpleHash pp(){
//        //通过用户的账号和密码先加、密
//        //获得关于账号为tomeh和密码的加密码
//        SimpleHash hash = new SimpleHash("MD5","1234","tome",1024);
//        return hash;
//    }
//    public    SimpleHash ll(Users user){
//        //通过用户的账号和密码先加密
//        //获得关于账号为tome和密码的加密码
//        SimpleHash hash = new SimpleHash("MD5",user.getUsername(),user.getPassword(),1024);
//        System.out.println("加密码"+hash);
//        //6d5b4811a4c3cb7f5731eb1371b8dd6e
//        return hash;
//    }

    public static void main(String[] args) {
        SimpleHash hash = new SimpleHash("MD5","1234","admin",1024);
        System.out.println(hash);
    }
}
